import { EncodeRulesService } from './../../../../../@core/services/encoderRule/encode-rules.service';
import { Component, Input } from '@angular/core';
import { PostDepartment } from 'src/app/@core/data/Department';
import { CascaderItem, PostDictionaryType } from 'src/app/pages/systemset/dirctory/dirctory';
@Component({
  selector: 'app-add-department',
  templateUrl: './add-department.component.html',
  styleUrls: ['./add-department.component.scss']
})
export class AddDepartmentComponent {
  @Input() data: any;
  options :any;//部门
  PostDepartment: PostDepartment={
    name: '',
    note: '',
    parentDepartmentId: null,
    departmentCode: '',
    enable: false
  };//新增部门
  items:any[]=[
    '启用', '禁用'
  ];
  currentSelectedItem = '启用';
  constructor(private EncodeRulesService:EncodeRulesService) { }
  ngOnInit(): void {
    this.PostDepartment.enable = this.currentSelectedItem == '启用' ? true : false
    this.options = this.convertToCascaderItem(this.data.options)
    if(this.data.pathPids){
      this.PostDepartment.parentDepartmentId = this.data.pathPids[this.data.pathPids.length-1]
    }
    this.EncodeRulesService.GenerateWarehouseCodeRule('部门编码','部门编码').subscribe((data:any)=>{
      this.PostDepartment.departmentCode = data
    })
  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.name,
        value: item.departmentId,
        icon: item.icon
      };
      if (item.subDepartments) {
        cascaderItem.children = this.convertToCascaderItem(item.subDepartments);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  onChanges($event: string | any[]){
    this.PostDepartment.parentDepartmentId = $event[$event.length-1]
  }
  valueChange(value: string): void {
    this.PostDepartment.enable = value == '启用' ? true : false
  }
}
